package test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.UUID;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.chaoyi.dao.mysql.CartoonChapterMapper;
import com.chaoyi.dao.mysql.CartoonMapper;
import com.chaoyi.model.Cartoon;
import com.chaoyi.model.CartoonChapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import net.sf.json.JSONArray;

public class catchPic {
	
	public static void main(String ages[]) throws Exception{
		System.out.println("start");
		catchImg();
		System.out.println("end");
	}
	
	public static void catchImg() throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		CartoonMapper cartoonMapper = context.getBean(CartoonMapper.class);
		CartoonChapterMapper cartoonChapterMapper =context.getBean(CartoonChapterMapper.class);
		try {
			// 添加方法忽略证书信任问题
			trustAllHttpsCertificates();
			HttpsURLConnection.setDefaultHostnameVerifier(hv);
			for (int i = 1; i <= 37; i++) {
				// 要抓取的图片库url
				String url = "http://hanman.lzhhhhh.com/info.aspx?id=" + i;
				System.out.println("开始抓取页面："+url);
				Document document = Jsoup.connect(url).get();
				// 获取Html文件中的body元素
				Element body = document.body();
				// 类选择器
				Element CartoonNameClassArray = body.getElementById("comicName");
				String CartoonName = CartoonNameClassArray.text();//名称
				Elements txtDescClassArray = body.getElementsByClass("txtDesc");//简介
				String txtDesc=txtDescClassArray.text();
				Elements pdClassArray = body.getElementsByClass("pd");//标签
				String pd=pdClassArray.text();
				
				Elements introNameClassArray = body.getElementsByClass("introName");//作者
				String introName=introNameClassArray.text();
				Cartoon cartoon = new Cartoon();
				
				Elements picClassArray = body.getElementsByClass("pic");//封面图
				Element img = picClassArray.select("img").first();//查找第一个a元素
				String src = img.attr("src");
				String arr[] = src.split("/");
				String str2 = URLEncoder.encode(arr[2],"UTF-8");
				str2 = str2.replace("+", "%20");
				String str3 = URLEncoder.encode(arr[3],"UTF-8");
				str3 = str3.replace("+", "%20");
				src = "/"+arr[1]+"/"+str2+"/"+ str3;
				src = "http://hanman.lzhhhhh.com"+src;
				//下载
				String savePath = "F:/漫画/";
				String fileName= UUID.randomUUID().toString()+".jpg";
				downloadHttpUrl(src,savePath,fileName);
				//上传
				uploadFile("F:/漫画/"+fileName,"cartoon/"+fileName);
				String pic = "http://mtp.myche51.cn/cartoon/"+fileName;
				System.out.println(pic);
				
				Elements listClassArray = body.select("script"); //章节内容
				String script = listClassArray.get(2).toString();
				script = (String) script.subSequence(script.indexOf("initIntroData")+15, script.length()-18);
				
				ObjectMapper mapper = new ObjectMapper();
				script = (String) script.subSequence(35, script.length()-7);
				script = "["+script+"]";
				JSONArray json = JSONArray.fromObject(script);
				
				cartoon.setIntroName(introName);
				cartoon.setName(CartoonName);
				cartoon.setPd(pd);
				cartoon.setPic("http://mtp.myche51.cn/cartoon/"+fileName);
				cartoon.setTxtDesc(txtDesc);
				CartoonChapter car=mapper.readValue(json.get(json.size()-1).toString(),CartoonChapter.class);
				cartoon.setChapterOrder(car.getChapter_order());
				cartoonMapper.saveCartoon(cartoon);
				System.out.println("图集入库成功，Id："+cartoon.getId());
				
				for(int k=0; k<json.size();k++){
					//获取章节信息
					CartoonChapter carC=mapper.readValue(json.get(k).toString(),CartoonChapter.class);
					String chapterUrl ="http://hanman.lzhhhhh.com/read.aspx?id="+carC.getComic_id()+"&cid="+carC.getId()+"&r=2033";
					Document chapterDocument = Jsoup.connect(chapterUrl).get();
					Element chapterBody = chapterDocument.body();
					
					String chapterScrpit = chapterBody.toString();
					chapterScrpit = (String) chapterScrpit.subSequence(chapterScrpit.indexOf("["), chapterScrpit.indexOf("]")+1);
					JSONArray chapterJson = JSONArray.fromObject(chapterScrpit);
					for(int j=0;j<100000;j++){
						try {
							String chapterDownPic = chapterJson.get(0).toString();
							String chapterArr[] = chapterDownPic.split("/");
							int temp = j+1;
							String te = URLEncoder.encode(chapterArr[2],"UTF-8");
							te = te.replace("+", "%20");
							chapterDownPic = "http://hanman.lzhhhhh.com/"+chapterArr[1]+"/"+te
											+"/"+chapterArr[3]+"/"+temp+".jpg";
							String chapterName= UUID.randomUUID().toString()+".jpg";
							downloadHttpUrl(chapterDownPic,savePath,chapterName);
							//上传
							uploadFile("F:/漫画/"+chapterName,"cartoon/chapter/"+chapterName);
							carC.setChapter_image("http://mtp.myche51.cn/cartoon/chapter/"+chapterName);
							carC.setCartoonId(cartoon.getId()+"");
							carC.setId(j+1+"");
							cartoonChapterMapper.saveCartoonChapter(carC);
							System.out.println("---图片入库成功，Id："+carC.getId());
						} catch (Exception e) {
							e.printStackTrace();
							break;
						}
					}
				}
			}
		} catch (
		IOException e)
		{
			e.printStackTrace();
		}
	}

	// 忽略证书方法1
	static HostnameVerifier hv = new HostnameVerifier() {
		public boolean verify(String urlHostName, SSLSession session) {
			// System.out.println("Warning: URL Host: " + urlHostName + " vs. "
			// + session.getPeerHost());
			return true;
		}
	};

	// 忽略证书方法2
	private static void trustAllHttpsCertificates() throws Exception {
		javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
		javax.net.ssl.TrustManager tm = new miTM();
		trustAllCerts[0] = tm;
		javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
		sc.init(null, trustAllCerts, null);
		javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
	}

	static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
				throws java.security.cert.CertificateException {
			return;
		}

		public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
				throws java.security.cert.CertificateException {
			return;
		}
	}
	
	/**
	 * //如果是Windows情况下，localFilePath格式是 D:\\qiniu\\test.png
	 * @param localFilePath
	 * @return
	 */
	public static void uploadFile(String localFilePath,String fileName){
	    //构造一个带指定Zone对象的配置类
	    Configuration cfg = new Configuration(Zone.zone0());
	    //...其他参数参考类注释
	    UploadManager uploadManager = new UploadManager(cfg);
	    //...生成上传凭证，然后准备上传
	    String accessKey = "HqeuYJi7RXkAKA1SIxXXFmtWVE61A5XcAh5_e1hy";
	    String secretKey = "R9G6oRkDSI7Oo1TnnUMP9HYhT6-TBbX0DxiomTVW";
	    String bucket = "xz-web";
	    //默认不指定key的情况下，以文件内容的hash值作为文件名
	    Auth auth = Auth.create(accessKey, secretKey);
	    String upToken = auth.uploadToken(bucket);
	    try {
	        Response response = uploadManager.put(localFilePath, fileName, upToken);
	        //解析上传成功的结果
	        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
	        System.out.println(putRet.key);
	        System.out.println(putRet.hash);
	    } catch (QiniuException ex) {
	        Response r = ex.response;
	        System.err.println(r.toString());
	        try {
	            System.err.println(r.bodyString());
	        } catch (QiniuException ex2) {
	        }
	    }
	}
	
    /**
	 * 下载文件---返回下载后的文件存储路径
	 * 
	 * @param url 文件地址
	 * @param dir 存储目录
	 * @param fileName 存储文件名
	 * @return
	 */
	public static void downloadHttpUrl(String url, String dir, String fileName) {
		try {
			URL httpurl = new URL(url);
			File dirfile = new File(dir);  
	        if (!dirfile.exists()) {  
	        	dirfile.mkdirs();
	        }
			FileUtils.copyURLToFile(httpurl, new File(dir+fileName));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
